package org.yuanzheng.wordcount

import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment}

/**
 * @author yuanzheng
 * @date 2020/6/10-21:25
 */
object StreamWordCount {
  def main(args: Array[String]): Unit = {
    // 1.初始化流计算环境
    val streamEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
    streamEnv.setParallelism(1) // 修改并行度
    // 2.读取数据
    val stream: DataStream[String] = streamEnv.socketTextStream("192.168.1.8", 8888)
    // 3.导入隐式转换
    import org.apache.flink.streaming.api.scala._
    // 4.转换和处理数据
    val result: DataStream[(String, Int)] = stream.flatMap(_.split(" ")).map((_, 1)).keyBy(0).sum(1)
    // 5.打印数据
    result.print()
    // 6.启动流计算程序
    streamEnv.execute("wordCount")
  }
}
